package com.zxmzy.scaffold.core.utils

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.telephony.TelephonyManager
import android.util.Log
import com.zxmzy.scaffold.core.ext.connectivityManager
import com.zxmzy.scaffold.core.ext.telephonyManager

/**
 * @author zhangxiaoming
 * @date 2023/7/28
 * @description
 */
@Suppress("DEPRECATION")
object NetworkUtil {

    /**
     * 网络可访问
     */
    fun isConnected(): Boolean {
        return connectivityManager?.let {
            var networkCapabilities = it.getNetworkCapabilities(it.activeNetwork)
            val isConnected =
                networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
            isConnected
        } ?: false
    }

    /**
     * 网络类型
     */
    @SuppressLint("MissingPermission")
    fun getNetworkType(): NetworkType {
        val cm = connectivityManager
        val isWifi: Boolean = cm?.let {
            val networkCapabilities = it.getNetworkCapabilities(it.activeNetwork)
            networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
        } ?: false
        if (isWifi) {
            return NetworkType.NETWORK_WIFI
        }
        val tm = telephonyManager
        return tm?.let {
            return when (it.networkType) {
                TelephonyManager.NETWORK_TYPE_GPRS,
                TelephonyManager.NETWORK_TYPE_GSM,
                TelephonyManager.NETWORK_TYPE_EDGE,
                TelephonyManager.NETWORK_TYPE_CDMA,
                TelephonyManager.NETWORK_TYPE_1xRTT,
                TelephonyManager.NETWORK_TYPE_IDEN -> NetworkType.NETWORK_2G

                TelephonyManager.NETWORK_TYPE_UMTS,
                TelephonyManager.NETWORK_TYPE_EVDO_0,
                TelephonyManager.NETWORK_TYPE_EVDO_A,
                TelephonyManager.NETWORK_TYPE_HSDPA,
                TelephonyManager.NETWORK_TYPE_HSUPA,
                TelephonyManager.NETWORK_TYPE_HSPA,
                TelephonyManager.NETWORK_TYPE_EVDO_B,
                TelephonyManager.NETWORK_TYPE_EHRPD,
                TelephonyManager.NETWORK_TYPE_HSPAP,
                TelephonyManager.NETWORK_TYPE_TD_SCDMA -> NetworkType.NETWORK_3G

                TelephonyManager.NETWORK_TYPE_LTE -> NetworkType.NETWORK_4G
                TelephonyManager.NETWORK_TYPE_NR ->NetworkType.NETWORK_5G
                TelephonyManager.NETWORK_TYPE_IWLAN -> NetworkType.NETWORK_WIFI
                else -> NetworkType.NETWORK_UNKNOWN
            }
        } ?: NetworkType.NETWORK_UNKNOWN
    }

    /**
     * 获取运营商
     */
    fun getOperatorName(): String {
        return telephonyManager?.simOperatorName ?: "未知"
    }

    /**
     * <4G网络
     */
    fun isWeakNetwork(): Boolean {
        if (!isConnected()) {
            return true
        }
        val type = getNetworkType()
        return type != NetworkType.NETWORK_WIFI && type != NetworkType.NETWORK_4G && type != NetworkType.NETWORK_5G
    }
}

enum class NetworkType {
    NETWORK_UNKNOWN, NETWORK_WIFI, NETWORK_2G, NETWORK_3G, NETWORK_4G, NETWORK_5G
}